package com.simple.datax.flow.preprocessor;

import java.util.List;

import com.alibaba.dubbo.common.logger.Logger;
import com.alibaba.dubbo.common.logger.LoggerFactory;
import com.simple.datax.flow.constants.ProcessResultConstants;
import com.simple.datax.flow.message.BaseEcMessage;
import com.simple.datax.flow.message.EcFlowMessage;
import com.simple.datax.flow.message.EcFlowRespMessage;
import com.simple.datax.flow.preprocessor.recvprotocol.TcsFlow201Protocol;
import com.simple.datax.flow.preprocessor.recvprotocol.TcsFlow201ResponseProtocol;
import com.simple.datax.flow.preprocessor.sendprotocol.TcsBPNoTransmissionProtocol;

/** 
 * @Title: TcsPreProcessorManager.java 
 * @Package com.simple.datax.flow.adapter 
 * @Description: TODO(EC和TCS协议管理器) 
 * @author tonney 
 * @date 2013-1-2 下午01:30:56 
 * @version V1.0 
 */
public class TcsPreProcessorManager implements PreProcessorService {
	public Logger log = LoggerFactory.getLogger(this.getClass());
	private TcsFlow201ResponseProtocol tcsFlow201ResProtocol;
	private TcsFlow201Protocol tcsFlow201Protocol;
	private TcsBPNoTransmissionProtocol tcsBpNoTransmissionProtocol;

	public void setTcsFlow201ResProtocol(
			TcsFlow201ResponseProtocol tcsFlow201ResProtocol) {
		this.tcsFlow201ResProtocol = tcsFlow201ResProtocol;
	}

	public void setTcsFlow201Protocol(TcsFlow201Protocol tcsFlow201Protocol) {
		this.tcsFlow201Protocol = tcsFlow201Protocol;
	}

	public void setTcsBpNoTransmissionProtocol(
			TcsBPNoTransmissionProtocol tcsBpNoTransmissionProtocol) {
		this.tcsBpNoTransmissionProtocol = tcsBpNoTransmissionProtocol;
	}

	@Override
	public int recvPreProcessor(BaseEcMessage ecMessage,List<BaseEcMessage> ecMessageList) {
		int result = ProcessResultConstants.STATUS_SUCCESS;
//		String msgType = null;
//		String activityNo = null;
		try{
			if(ecMessage instanceof EcFlowMessage){
				EcFlowMessage ecFlowMessage = (EcFlowMessage)ecMessage;
				ecFlowMessage.getFlowMessageHead().getMessageType();
				ecFlowMessage.getFlowMessageBody().getActivityIdList().get(0);
				result = tcsFlow201Protocol.process(ecFlowMessage, ecMessageList);				
			}else if(ecMessage instanceof EcFlowRespMessage){
				EcFlowRespMessage ecFlowRespMessage = (EcFlowRespMessage)ecMessage;
//				msgType = ecFlowRespMessage.getFlowRespMessageHead().getMessageType();
//				activityNo = ecFlowRespMessage.getFlowRespMessageBody().getActionResultList().get(0).getActionId();				
				result = tcsFlow201ResProtocol.process(ecFlowRespMessage, ecMessageList);
			}else{
				result = ProcessResultConstants.STATUS_RECV_PROCESS_EXCEPTION;
				log.error("TCSPreProcessorManager::recvPreProcessor unknow tcs protocol msgType");
			}
		}catch(Exception e){
			result = ProcessResultConstants.STATUS_RECV_PROCESS_EXCEPTION;
			log.error("TCSPreProcessorManager::recvPreProcessor error=>"+e.toString());
		}
		return result;
	}

	@Override
	public int sendPreProcessor(EcFlowMessage ecMessage) {
		int result = ProcessResultConstants.STATUS_SUCCESS;
		try{
			result = tcsBpNoTransmissionProtocol.process(ecMessage);
		}catch(Exception e){
			result = ProcessResultConstants.STATUS_SEND_PROCESS_EXCEPTION;
			log.error("TCSPreProcessorImpl::sendPreProcessor error=>"+e.toString());
		}
		return result;
	}
}
